import requests
from CompareTime import times
# url = "http://39.99.191.237:8603/flagAppApi/api/flagUser/memberLoginByUsername"
# data = {"username":13711110897,"password":'200820E3227815ED1756A6B531E7E0D2'}
def seesion():
    seesion = requests.session()
    return seesion
def register(url,mobile):
    url = url
    header = {'content-type':'application/json','accept':'*/*'}
    data = {"mobile":mobile,"mobileCode":888888}
    re = requests.post(url=url,headers=header,json=data).json()
    token = re['result']['token']
    return token

def createFlag(url,token,flagName,holidayTime,insistDay,startDate):
    header = {'content-type':'application/json','accept':'*/*','Authorization':token}
    url = url
    data = {"flagName":flagName,"challengeMoney":100,"holidayTime":holidayTime,"insistDay":insistDay,"startDate":startDate}
    res = requests.post(url=url,headers=header,json=data)
    return res.json()['result']

def flagPay(url,id,money,paytype,token):
    header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization':token}
    url = url
    data = {"challengeMoney":money,"flagId":id,"payType":paytype}
    res = requests.post(url=url,headers=header,json=data).json()
    return res['ret']

def flagDetails(id,token):
    header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization': token}
    data = {"flagId":id}
    url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/flag/getInfo'
    res =requests.post(url=url,headers=header,json=data).json()
    return res

def userFlags(number,size,status,token):
    header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization': token}
    data = {"pageNumber":number,"pageSize":size,"status":status}
    url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/flag/getListByUser'
    res =requests.request('post',url=url,headers=header,json=data).json()
    lists =  res["result"]["list"]
    # print(lists)
    list = []
    for item in lists:
        id= item['id']
        flagName = item['flagName']
        status = item['status']
        statuss = statusvalue(status)
        list.append({'id':id,'flagName':flagName,'status':statuss})
    return list

def flagList(number,size,token):
    header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization': token}
    data = {"pageNumber":number,"pageSize":size}
    url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/flag/getList'
    res =requests.request('post',url=url,headers=header,json=data).json()
    lists =  res["result"]["list"]
    list = []
    for item in lists:
        id= item['id']
        flagName = item['flagName']
        status = item['status']
        statuss = statusvalue(status)
        list.append({'id':id,'flagName':flagName,'status':statuss})
    return lists

def punchCard(cardcontent,id,type,token):
    header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization': token}
    data = {"content":cardcontent,"flagId":id,"type":type}
    url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/clockInRecord/punchIn'
    res =requests.request('post',url=url,headers=header,json=data).json()
    return res['desc']

def punchCardDetail(id,number,size,type):
    header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization': token}
    data = {"flagId":id,"pageNumber":number,"pageSize":size,"type":type}
    url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/clockInRecord/clockIndynamic'
    res = requests.request('post',url=url, headers=header, json=data).json()
    print(res)
    return res['result']['list'][0]['originalDate']

def statusvalue(item):
    resurtitem = int(item)
    if resurtitem == 1:
        resurtitem = "进行中"
    elif resurtitem==2:
        resurtitem = "挑战失败"
    elif resurtitem ==3:
        resurtitem = "挑战成功"
    elif resurtitem ==4 or resurtitem ==7:
        resurtitem = "不服再战中"
    elif resurtitem == 5:
        resurtitem = "再战失败"
    elif resurtitem ==6:
        resurtitem = '再战成功'
    return resurtitem

if __name__ == '__main__':
    token = register("https://hysz-test.huayunshuzi.com/flagAppApi/api/flagUser/mobileCodeLogin",13711110897)
    print(token)
    # flagid = flag("https://hysz-test.huayunshuzi.com/flagAppApi/api/flag/createFlag",token,"这是我用接口创建的第一个Flag",5,30,'2021-08-25 11:32:07')
    # pay = pay("https://hysz-test.huayunshuzi.com/flagAppApi/api/pay/setChallengeMoney",flagid,10,2,token)
    # print(pay)
    # details = flagDetails(1147,token)
    # print(details)
    # userdetail = userFlags(1,50,1,token)
    # print(userdetail)
    # print(userdetail)
    # a = flagList(1,5,token)
    # print(a)
    # id = userdetail[0]['id']
    # print(id)
    # print(punchCard("接口进行打卡",id,0,token))
    a= punchCardDetail(1174,1,5,1)
    print(a)
    print(times(a))